package _220321;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author ShadowLim
 * @create 2022-03-21-8:42
 */
public class 走迷宫 {
    static int n, m;
    static int[][] dirs = {{0, 1}, {0, -1}, {1, 0} , {-1, 0}};
    static int[][] graph = new int[101][101];
    static boolean[][] vis = new boolean[101][101];

    static class Node  {
        int x, y;
        int step;
    }

    static Node node1 = new Node();
    static Node node2 = new Node();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                graph[i][j] = scanner.nextInt();
            }
        }

        node1.x = scanner.nextInt();
        node1.y = scanner.nextInt();
        node2.x = scanner.nextInt();
        node2.y = scanner.nextInt();
        scanner.close();
        System.out.println(bfs());
    }

    private static int bfs() {
        Queue<Node> q = new ArrayDeque<>();
        q.offer(node1);
        while (!q.isEmpty()) {
            Node top = q.poll();
            if (top.x == node2.x && top.y == node2.y) {
                return top.step;
            }
            for (int i = 0; i < 4; i++) {
                int nx = top.x + dirs[i][0];
                int ny = top.y + dirs[i][1];
                if (check(nx,ny)) {
                    Node temp = new Node();
                    temp.x = nx;
                    temp.y = ny;
                    temp.step = top.step + 1;
                    q.offer(temp);
                    vis[nx][ny] = true;
                }
            }
        }
        return -1;
    }

    private static boolean check(int x, int y) {
        if (x < 1 || x > n || y < 1 || y > m) {
            return false;
        }
        if (graph[x][y] == 0) {
            return false;
        }
        if (vis[x][y] == true) {
            return false;
        }
        return true;
    }
}
